package com.zmn.oms.business.impl.base;

import com.google.common.collect.Lists;
import com.zmn.base.product.common.consts.BaseProductConsts;
import com.zmn.base.product.common.dto.product.ec.DefaultEcProductQuery;
import com.zmn.base.product.common.dto.product.ec.ProductBaseDRO;
import com.zmn.base.product.dubbo.interfaces.product.foreign.ProductForeignListRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.oms.business.interfaces.base.BasePlatService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 类描述：基础平台数据处理
 * @author heciqi
 * @date 2019/9/26 11:35
 */
@Slf4j
@Service
public class BasePlatServiceImpl implements BasePlatService {
	@Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
	ProductForeignListRemoteService productForeignListRemoteService;

	/**
	 * 获取前端产品
	 * @param productId 产品id
	 * @param showType  产品类型 1 后端产品，2 前端产品
	 * @return
	 */
	@Override
	public Integer getShowProductId(Integer productId, Integer showType) {
		Integer showProductId = null;
		if (Objects.equals(BaseProductConsts.EC_SHOW_TYPE, showType)) {
			showProductId = productId;
		} else {
			// 后端产品查询前端产品
			DefaultEcProductQuery defaultEcProductQuery = new DefaultEcProductQuery();
			defaultEcProductQuery.setProductIdList(Lists.newArrayList(productId));
			defaultEcProductQuery.setShowType(BaseProductConsts.EC_SHOW_TYPE);
			ResponseDTO<List<ProductBaseDRO>> showProductRemote = productForeignListRemoteService.listEcErpProductByQuery(defaultEcProductQuery);
			if (!CollectionUtils.isEmpty(showProductRemote.getData())) {
				showProductId = showProductRemote.getData().get(0).getProductId();
			} else {
				log.error("产品数据配置有误，[{}}]没有映射前端产品", productId);
			}
		}
		return showProductId;
	}
}
